<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button @click="handleAdd">新增</el-button>
        <el-button  @click="translation">打印</el-button>
      </el-col>
      <right-toolbar @queryTable="getList"></right-toolbar>
    </el-row>

    <vxe-table
      :data="tableData.list"
      :loading="tableData.loading"
      border
      stripe
      :pager-config="{ pageSize: tableData.paging.pageSize, total: tableData.paging.total }"
      @page-change="getList"
    >
      <vxe-column field="status" title="状态" width="100" align="center">
        <template #default="{ row }">
          <dict-tag :options="apply_state" :value="row.state" />
        </template>
      </vxe-column>
      <vxe-column field="number" title="编号">
        <template #default="{ row }">
          <el-button style="margin-right: 10px;" link type="primary" size="small" @click="handleDetail(row)">
            {{ row.number }}
          </el-button>
        </template>
      </vxe-column>
      <vxe-column field="num" title="数量"></vxe-column>
      <vxe-column field="createUserName" title="申请人"></vxe-column>
      <vxe-column field="remark" title="备注"></vxe-column>

      <vxe-column title="操作" align="center" class-name="small-padding fixed-width">
        <template #default="{ row }">
          <el-button style="margin-right: 10px;" link type="primary" size="small" v-if="row.state === 0 || row.state === 3" @click="handleSub(row)">提交</el-button>
          <el-button style="margin-right: 10px;" link type="primary" size="small" v-if="row.state === 0 || row.state === 3" @click="handleUpdate(row)">修改</el-button>
          <el-button v-if="row.state === 0 || row.state === 3" size="small" link type="primary" @click="handleDelete(row)">删除</el-button>
        </template>
      </vxe-column>
    </vxe-table>

    <pagination
      v-show="tableData.paging.total > 0"
      :total="tableData.paging.total"
      v-model:page="tableData.paging.pageNum"
      v-model:limit="tableData.paging.pageSize"
      @pagination="getList"
    />

    <el-dialog v-model="isAssetDialogVisible" width="50%">
      <asset-dialog ref="assetDialog" @sub="getList"></asset-dialog>
    </el-dialog>

    <edit :apply-type="applyType" ref="edit" @sub="getList"></edit>

    <detail ref="detailDialog"></detail>
  </div>
</template>


<script setup>
import api from "./api.js";
import Edit from './edit.vue'
import Detail from "@/components/Asset/ApplyDetailDialog/index";
import AssetDialog from "@/views/asset/apply/in/storages.vue";

const edit = ref();
const isAssetDialogVisible = ref(false);
const detailDialog = ref();

const { proxy } = getCurrentInstance();
const { apply_state } = proxy.useDict("apply_state");

// 申请类型
const applyType = ref('ZCRK');

const queryParams = reactive({
  applyType: applyType.value,
});

const tableData = reactive({
  loading: false,
  list: [], // 表格列表数据
  paging: {
    total: 0, // 总条数
    pageNum: 1,
    pageSize: 10,
  }
});

function getList() {
  tableData.loading = true;
  const {pageNum, pageSize} = tableData.paging;

  const query = {...queryParams};
  const pageReq = { page: pageNum - 1, size: pageSize };
  api.list(query, pageReq).then(response => {

    const { content, totalElements } = response
    if (content && content.length > 0) {
      content.forEach(item => {
        if (item.formData) {
          let formData = JSON.parse(item.formData);
          Object.assign(item, formData);
        }
      })
    }

    tableData.list = content;
    tableData.paging.total = totalElements;
    tableData.loading = false;
  });
}

/** 新增按钮操作 */
function handleAdd() {
  edit.value.open();
}

function translation() {
  isAssetDialogVisible.value = true;
}

/** 修改按钮操作 */
function handleUpdate(row) {
  edit.value.open(row);
}

function handleDetail(row) {
  detailDialog.value.open(row);
}

function handleSub(row) {
  api.sub(row.id).then(()=> {
    getList();
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除').then(function() {
    return api.del(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

onMounted(()=> {
  getList()
})

</script>
  
<style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
  height: 92vh; 
  overflow: hidden; 
}

.vxe-table {
  flex: 1;
  overflow: auto;
}

.pagination-container {
  display: flex;
  justify-content: flex-end; 

}
</style>